import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:search_people/bean/search/hot_people.dart';
import 'package:search_people/entrance/route/routes.dart';
import 'package:search_people/page/home/main/logic.dart';
import 'package:search_people/utils/component/carousel_text.dart';
import 'package:search_people/value/topcolors.dart';

class HotPeopleWidget extends GetView<HomeLogic> {
  const HotPeopleWidget({Key? key}) : super(key: key);

  List<HotPeople> _showHots() {
    // if (controller.state.hots.length > 4) {
    //   return controller.state.hots.sublist(0, 4);
    // }
    return controller.state.hots;
  }

  Widget _buildItems(
    String text, {
    Function()? onTap,
  }) {
    return InkWell(
      onTap: onTap,
      child: Text(
        text,
        style: TextStyle(color: Colors.white, fontSize: 12.sp),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      // color: TopColors.main_color,
      padding: EdgeInsets.fromLTRB(30.r, 0, 12.r, 10.r),
      child: Row(
        // crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          InkWell(
            onTap: () {
              Get.toNamed(RouterPath.hot_people,
                  arguments: controller.state.hots);
            },
            child: Container(
              width: 50.r,
              height: 80.r,
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(3.r),
                color: TopColors.main_red_deep,
              ),
              alignment: Alignment.center,
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Text(
                    '热门',
                    style: TextStyle(color: Colors.white, fontSize: 13.sp),
                  ),
                  Text(
                    '人物',
                    style: TextStyle(color: Colors.white, fontSize: 13.sp),
                  )
                ],
              ),
            ),
          ),
          SizedBox(
            width: 20.r,
          ),
          GetBuilder<HomeLogic>(
            builder: (controller) {
              return Expanded(
                child: CarouselText(
                    list: _showHots()
                        .map((e) =>
                            ((e.currentPosition ?? '') + (e.leaderName ?? '')))
                        .toList(),
                    onTap: (p0) {
                      var e = _showHots()[p0];
                      Get.toNamed(RouterPath.person_detail,
                          arguments: e.leaderId);
                    },
                    height: 80.r),
              );
              return Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: _showHots()
                    .asMap()
                    .map((key, e) => MapEntry(
                        key,
                        _buildItems(
                          key > 2
                              ? "查看更多>"
                              : (e.currentPosition ?? '') +
                                  (e.leaderName ?? ''),
                          onTap: () {
                            if (key > 2) {
                              Get.toNamed(RouterPath.hot_people,
                                  arguments: controller.state.hots);
                              return;
                            }
                            Get.toNamed(RouterPath.person_detail,
                                arguments: e.leaderId);
                          },
                        )))
                    .values
                    .toList(),
              );
            },
          )
        ],
      ),
    );
  }
}
